








package com.meow_woof.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.meow_woof.constant.MessageConstant;
import com.meow_woof.constant.StatusConstant;
import com.meow_woof.dto.SetmealDTO;
import com.meow_woof.dto.SetmealPageQueryDTO;
import com.meow_woof.entity.Dish;
import com.meow_woof.entity.Setmeal;
import com.meow_woof.entity.SetmealDish;
import com.meow_woof.exception.DeletionNotAllowedException;
import com.meow_woof.mapper.DishMapper;
import com.meow_woof.mapper.SetmealDishMapper;
import com.meow_woof.mapper.SetmealMapper;
import com.meow_woof.result.PageResult;
import com.meow_woof.service.SetmealService;
import com.meow_woof.vo.DishItemVO;
import com.meow_woof.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //向套餐表插入一条数据
        setmealMapper.insert(setmeal);
        //获取insert语句生成的主键值
        Long setmealId=setmeal.getId();
        //向套餐菜品表插入n条数据
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        if (setmealDishes!=null && setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish ->   setmealDish.setSetmealId(setmealId));
            //向套餐菜品表插入n条数据
            setmealDishMapper.insertBatch(setmealDishes);
        }



    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //select * from setmeal limit 0,10
        //开始分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> records=page.getResult();
        return new PageResult(total,records);
    }
    /**
     * 套餐批量删除
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        ////判断当前套餐是否能被删除--是否存在起售中的套餐？？
        for(Long id:ids){
            Setmeal setmeal=setmealMapper.selectById(id);
            if(setmeal.getStatus()== StatusConstant.ENABLE){
                //当前套餐起售中，不能删除
                throw new DeletionNotAllowedException((MessageConstant.SETMEAL_ON_SALE));

            }
        }
        //根据套餐id删除套餐数据
        //sql：delete from setmeal where id in(?,?,?)
        setmealMapper.deleteByIds(ids);
        //根据套餐id集合批量删除关联的菜品数据
        //sql:delete from setmeal_dish where setmeal_id in(?,?,?)
        setmealDishMapper.deleteBySetmealIds(ids);

    }
    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDishes(Long id) {

        Setmeal setmeal=setmealMapper.selectById(id);
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);

        List<SetmealDish> dishes=setmealDishMapper.getDishesBySetmealId(id);
        setmealVO.setSetmealDishes(dishes);


        return setmealVO;
    }

    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        //修改套餐表基本数据
        setmealMapper.update(setmeal);
        //删除原有的套餐菜品
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        //重新插入菜品数据
        if (setmealDishes!=null&&setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
        //向菜品表加入n条数据
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }
    /**
     * 起售，停售套餐
     * @param status
     * @return
     */
    @Override
    public void startOrStop(Integer status,Long id) {
        Setmeal setmeal=Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        if(status==StatusConstant.ENABLE){
            List<SetmealDish> setmealDishes=setmealDishMapper.getDishesBySetmealId(id);
            for(SetmealDish setmealDish:setmealDishes){
                Long dishId=setmealDish.getDishId();

                Dish dish=dishMapper.selectById(dishId);
                Integer dishStatus=dish.getStatus();
                if (dishStatus==StatusConstant.DISABLE){
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);


                }
            }
        }
        setmealMapper.update(setmeal);



    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }
    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
